
class Admin::ProductsController < Admin::BaseController

  before_action :find_product, only: [:edit, :update, :destroy, :enable_top_or_disable_top, :set_on_or_off]

  def index
     #authorize! :update, current_user
    @products = Product.unscoped.paginate(page: params[:page] || 1, per_page: params[:per_page] || 15).order("id desc")
  end

  def new
    @product = Product.new
    @product.build_stock
    @root_categories = Category.roots
  end

  def create
    #@product = Product.new(params.require(:product).permit(Product.attribute_names, stock_attributes: [:amount, :unit], properties_attributes: [:item_type, :item_value]).except(:amount))
    @product = Product.new(params.require(:product).permit(Product.attribute_names, :category_ids => []))
    @product.build_stock amount: params[:product][:amount]
    if params[:product][:properties].present?
      params[:product][:properties].each do |property|
        @product.properties.build({item_type: property[:item_type], item_value: property[:item_value]})
      end
    end

    if @product.save
      flash[:notice] = "创建成功"
      redirect_to admin_products_path
    else
      @root_categories = Category.roots
      render action: :new
    end
  end

  def edit
    @root_categories = Category.roots
    render action: :new
  end

  def update
    @product.attributes = params.require(:product).permit(Product.attribute_names << {category_ids: []}).except(:amount)
    @root_categories = Category.roots
    #do with transaction
    flag = false
    ApplicationRecord.transaction do
      @product.stock.update_attributes!(amount: params[:product][:amount], unit: params[:product][:stock][:unit].to_i)
      @product.properties.each(&:destroy!)
      if params[:product][:properties]
        params[:product][:properties].each_with_index do |property, index|
          @product.properties.build({item_type: property[:item_type], item_value: property[:item_value]})
        end
      end
      @product.save!
      flag = true
    end
    if flag
      flash[:notice] = "修改成功"
      redirect_to admin_products_path
    else
      render action: :new
    end
  end

  def destroy
    if @product.destroy
      flash[:notice] = "删除成功"
      redirect_to admin_products_path
    else
      flash[:notice] = "删除失败"
      redirect_to :back
    end
  end

  def get_data
    @products = Product.paginate(page: params[:page] || 1, per_page: params[:per_page] || 15).order("updated_at DESC")
    if @products
      render json: {
        status: 'ok',
        products: wrap_pagination_json(@products)
      }
    else
      render json: {
        status: 'error',
        msg: '未拉取到数据'
      }
    end
  end

  def enable_top_or_disable_top
    @product.is_top = !@product.is_top
    @product.save
    redirect_to action: :index
  end

  def set_on_or_off
    if @product.on?
      @product.off!
    else
      @product.on!
    end
    redirect_to action: :index
  end

  private

  def find_product
    @product = Product.unscoped.find(params[:id])
  end

end

